module.exports = class extends baseLogic {
    constructor() {
        super();
    }

    static init() {
        return new this();
    }

    async getItems(FindFilterModel, CurPage, PageSize, Sort) {
        let ret = {};
        let PartnersItems = {
            CurPage: CurPage,
            PageSize: PageSize
        };
        let PlatformRuleItems = [];
        let FindFilter = {
            OS_IsDel: 0
        };
        if (FindFilterModel.OS_Name) {
            FindFilter.OS_Name = {
                '$like': '%' + FindFilterModel.OS_Name + '%'
            };
        }



        if (FindFilterModel.OS_CreateTime) {
            FindFilter.OS_CreateTime = {
                '$between': FindFilterModel.OS_CreateTime.split(',')
            };
        }

        if (FindFilterModel.OS_PlatFormID > 0) {
            FindFilter.OS_PlatFormID = FindFilterModel.OS_PlatFormID;
            PlatformRuleItems = await this.DB.iframe_platform_rule.Access.findAll({
                attributes: ['RuleID', 'RuleName'],
                where: {
                    RulePlatformID: FindFilterModel.OS_PlatFormID,
                    IsDel: 0
                },
                raw:true
            });
        }

        if (FindFilterModel.OS_PlatformRuleID > 0) {
            FindFilter.OS_PlatformRuleID = FindFilterModel.OS_PlatformRuleID;
        }

        let data = await this.DB.v_iframe_partners.Access.findAndCountAll({
            attributes: ['OS_ID', 'OS_Name', 'OS_IsDel', 'OS_PlatformRuleID', 'OS_PlatFormID', 'OS_CreateTime', 'RuleName', 'RulePlatformName'],
            where: FindFilter,
            order: [
                Sort.split('|')
            ],
            offset: (CurPage - 1) * PageSize,
            limit: PageSize,
            raw:true
        });
        let DataList = data.rows;
        DataList.map( (item)=> {
            item.OS_CreateTime = this.moment(item.OS_CreateTime).format("YYYY-MM-DD HH:mm:ss");
        });
        PartnersItems.RsCount = data.count;
        PartnersItems.DataList = DataList;
        let PlatformItems = await this.DB.iframe_platform.Access.findAll({
            where:{
                IP_IsDel: 0
            },
            raw:true
        });

        return {
            PartnersItems: PartnersItems,
            PlatformItems:PlatformItems,
            PlatformRuleItems: PlatformRuleItems
        }

    }

    async getItem(id){
        let ret={
            Model:{}
        };
        if(id>0){
            ret.Model = await this.DB.iframe_orgstruct.Access.findOne({
                where:{
                    OS_ID:id
                },
                raw:true
            });
        }

        ret.PlatformItems = await this.DB.iframe_platform.Access.findAll({
            attributes: ['IP_ID', 'IP_Name'],
            where: {
                IP_IsDel: 0
            },
            raw:true
        });
        ret.PlatformRuleItems = await this.DB.iframe_platform_rule.Access.findAll({
            attributes: ['RuleID', 'RuleName', 'RulePlatformID'],
            where: {
                IsDel: 0
            },
            raw:true
        });
        return ret;
    }

    async insertOrUpdate(body){
        let id = body.OS_ID ? Number(body.OS_ID) : 0;
        var model = {};
        if (id > 0) {
            model = await this.DB.iframe_orgstruct.Access.findById(id);
            if (!model) {
                return null;
            }
            model = model.dataValues;
        } else {
            model.OS_ID = 0;
            model.OS_IsDel = 0;
            model.OS_IsParent=0;
            model.OS_ParentID=0;
            model.OS_Type='001';//企业
            model.OS_Sort=0;
            model.OS_IsShow=1;
            model.OS_CreateTime = this.moment().format("YYYY-MM-DD HH:mm:ss");
        }
        model.OS_Code=body.OS_Code;
        model.OS_Name = body.OS_Name;
        model.OS_Admin_UserID = body.OS_Admin_UserID;
        model.OS_Addr = body.OS_Addr;
        model.OS_Desc = body.OS_Desc;
        model.OS_AreaCode=body.OS_AreaCode;
        model.OS_PlatformRuleID = body.OS_PlatformRuleID;
        model.OS_PlatFormID=body.OS_PlatFormID;
        model.OS_PartnersID = body.OS_PartnersID;

        if (id > 0) {
            model= await this.DB.iframe_orgstruct.Access.update(model, {
                where: {
                    OS_ID: id
                }
            });
        } else {
            model = await this.DB.iframe_orgstruct.Access.create(model);
            model.OS_PartnersID=model.OS_ID;
            model.save();

            var adminUser = {
                UserName: 'admin:' + model.OS_ID,
                UserRealName: '企业超级管理员',
                UserJobNum: '0000000',
                UserPwd: this.webTools.common.GetMD5('123456'),
                UserMobilePhone: '',
                UserRule: '',
                UserLoginIp: '',
                UserPingYin: 'cjgly',
                UserCreatetime: this.moment().unix(),
                UserLoginTime: this.moment().unix(),
                UserIsSuper: 1,
                UserIsOnline: 0,
                UserIsDel: 0,
                UserAccessToken: '',
                UserPartnersID: model.OS_ID,
                UserOrgStructID:model.OS_ID

            };
            await this.DB.iframe_user.Access.create(adminUser);
        }

        return model;

    }

    async del(keys){
        return await this.DB.iframe_orgstruct.Access.update({
            OS_IsDel: 1
        }, {
            where: {
                OS_ID: keys
            }
        });
    }


    async getAllItems(){
        let ret={};
        ret.platformItems=await this.DB.iframe_orgstruct.Access.findAll({
            where:{
                IP_IsDel:0
            },
            raw:true
        });

       ret.partnersItems = await this.DB.iframe_orgstruct.Access.findAll({
            where: {
                IP_IsDel: 0
            },
            raw:true
        });

        return ret;
    }

};
